﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acme.Sakila.TestSite.Objects;
using nHydrate.EFCore.DataAccess;
using Acme.Sakila.EFDAL;
using Acme.Sakila.EFDAL.Entity;

namespace Acme.Sakila.TestSite
{
	public partial class PaymentList : BasePage
	{
		#region Page Events

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			grdItem.RowDataBound += new GridViewRowEventHandler(grdItem_RowDataBound);
			this.PagingControl1.ObjectSingular = "Payment";
			this.PagingControl1.ObjectPlural = "Payments";
			this.PagingControl1.RecordsPerPageChanged += new EventHandler(PagingControl1_RecordsPerPageChanged);
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if (!this.IsPostBack)
			{
				this.Populate();
			}
		}

		#endregion

		#region Methods

		private void Populate()
		{
			using (var context = new SakilaEntities(Utils.ContextStartup))
			{
				List<payment> paymentCollection = null;
				var paging = new Paging(this.PagingControl1.PageIndex, this.PagingControl1.RecordsPerPage);

				if (this.Request["customerid"] != null)
				{
					var customerId = this.Request["customerid"].ToInt32();
					var theCustomer = context.customer.FirstOrDefault(x => x.customer_id == customerId);
					paymentCollection = context.payment.GetPagedResults(x => x.customer_id == customerId, x => x.payment_id, paging).ToList();
					lblHeader.Text = "This is a list of all payments for customer <a href=\"/CustomerItem.aspx?id=" + customerId + "\">" + theCustomer.first_name + " " + theCustomer.last_name + "</a>.";
				}
				else if (this.Request["rentalid"] != null)
				{
					var rentalId = this.Request["rentalid"].ToInt32();
					var theRental = context.rental.FirstOrDefault(x => x.rental_id == rentalId);
					paymentCollection = context.payment.GetPagedResults(x => x.rental_id == rentalId, x => x.payment_id, paging).ToList();
					lblHeader.Text = "This is a list of all payments for rental <a href=\"/RentalItem.aspx?id=" + rentalId + "\">[" + theRental.rental_id + "]</a>.";
				}
				else
				{
					paymentCollection = context.payment.GetPagedResults(x => x.payment_id, paging).ToList();
					lblHeader.Text = "This is a list of all payments.";
				}

				this.PagingControl1.ItemCount = paging.RecordCount;
				grdItem.DataSource = paymentCollection;
				grdItem.DataBind();
				SessionHelper.LastPaymentListSearch = this.Request.Url.AbsoluteUri;
			}
		}

		#endregion

		#region Event Handlers

		private void grdItem_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				var thePayment = e.Row.DataItem as Acme.Sakila.EFDAL.Entity.payment;
				var linkEdit = (HyperLink)e.Row.FindControl("linkEdit");
				var linkCustomer = (HyperLink)e.Row.FindControl("linkCustomer");

				linkEdit.NavigateUrl = "/PaymentItem.aspx?id=" + thePayment.payment_id;
				linkCustomer.Text = thePayment.customer.last_name;
				linkCustomer.NavigateUrl = "/CustomerItem.aspx?id=" + thePayment.customer_id;
			}
		}

		private void PagingControl1_RecordsPerPageChanged(object sender, EventArgs e)
		{
			var query = new URL(this.Request.Url.AbsoluteUri);
			query.Parameters.SetValue("rpp", PagingControl1.RecordsPerPage.ToString());
			query.Parameters.SetValue("po", "1");
			this.Response.Redirect(query.ToString());
		}

		#endregion

	}
}